package com.xintonghua.bussiness.ui.fragment.client;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xintonghua.base.widget.ShowAllListView;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.api.HttpCent;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.BackTypeBean;
import com.xintonghua.bussiness.bean.Contant;
import com.xintonghua.bussiness.bean.GetModeDetailsBean;
import com.xintonghua.bussiness.util.AbstractBaseAdapter;
import com.xintonghua.bussiness.util.JsonUtil;
import com.xintonghua.bussiness.util.JumpUtils;
import com.xintonghua.bussiness.util.MyState;
import com.xintonghua.bussiness.util.MyUtils;
import com.xintonghua.bussiness.util.ViewHolder;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 顾客回访
 * 作者：wanxianyi
 * 时间：2018/3/5.
 * 包名：com.xintonghua.bussiness.ui.fragment.client
 * <p>
 * 回访客户
 * 更改和完善：liqi
 * 时间：2018/3/29.
 * 包名：com.xintonghua.bussiness.ui.fragment.client
 */

public class CustomersActivity extends BaseActivity implements initMView {
    @BindView(R.id.ll_select_customer)
    LinearLayout llSelectCustomer;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.et_text)
    EditText etText;
    @BindView(R.id.tv_save_moban)
    TextView tvSaveMoban;
    @BindView(R.id.slistview)
    ShowAllListView slistview;

    List<BackTypeBean> list;
    AbstractBaseAdapter adapter;
    @BindView(R.id.btn_cancel)
    Button btnCancel;
    @BindView(R.id.btn_send)
    Button btnSend;

    Contant beforeContant;//上一页的Contant，主要用于一进去就选好人
    Contant userInfos;//下一个页面传进来的

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.actvity_customers);
        ButterKnife.bind(this);
        list = new ArrayList<>();
        beforeContant = (Contant) JumpUtils.getSerializable(this);

        if (beforeContant != null)
        {
            userInfos = beforeContant;
            tvName.setText(""+userInfos.getNickname());
        }
        initUI();
    }

    @Override
    public void initUI() {
        super.initUI();
        onSimpleActionBar();
        setSimpleActionBar("回访客户");
        list = new ArrayList<>();
        slistview.setAdapter(adapter = new AbstractBaseAdapter<BackTypeBean>(list) {
            @Override
            public View getAdapterViewAtPosition(int position, View convertView, ViewGroup parent) {

                if (convertView == null) {
                    convertView = LayoutInflater.from(CustomersActivity.this).inflate(R.layout.activity_customers, null);
                }
                BackTypeBean bean = getItem(position);
                TextView tv_type = ViewHolder.get(convertView, R.id.tv_type);
                TextView tv_numbers = ViewHolder.get(convertView, R.id.tv_numbers);
                tv_type.setText("" + bean.getName() + ":");
                tv_numbers.setText("");
                return convertView;
            }
        });
        slistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                JumpUtils.jumpActivityForResult(CustomersActivity.this, GetModeDetailsActivity.class, (Serializable) list.get(i), MyState.requestThree, false);
            }
        });
        tvSaveMoban.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(etText.getText().toString().trim())) {
                    MyUtils.mToast(CustomersActivity.this, "模板内容不能为空");
                    return;
                }
                HashMap<String, String> map = new HashMap<>();
                map.put("content", etText.getText().toString().trim());
                JumpUtils.jumpto(CustomersActivity.this, SaveModeToActivity.class, (Serializable) list, map);
            }
        });
        getBackMemberList();
    }

    @OnClick({R.id.ll_select_customer, R.id.btn_cancel, R.id.btn_send})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_select_customer:
                JumpUtils.jumpActivityForResult(CustomersActivity.this, SelectCustomerActivity.class, null, MyState.requestOne);
                break;
            case R.id.btn_cancel:
                finish();
                break;
            case R.id.btn_send:
                if (userInfos == null || TextUtils.isEmpty(etText.getText().toString().trim())) {

                    MyUtils.mToast(CustomersActivity.this, "未选择访问客服或访问内容为空!");
                    return;
                }
                HttpCent.getInstance(this).sendRevistRecord("" + userInfos.getId(), etText.getText().toString().trim(), CustomersActivity.this, MyState.requestFive);

                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //访问的用户
        if (requestCode == MyState.requestOne && resultCode == Activity.RESULT_OK) {
            if (data != null) {
                userInfos = (Contant) data.getSerializableExtra("data");
                tvName.setText("" + userInfos.getNickname());
            }

        }
//问候模板
        if (requestCode == MyState.requestThree && resultCode == Activity.RESULT_OK) {
            if (data != null) {
                GetModeDetailsBean str = (GetModeDetailsBean) data.getSerializableExtra("data");
                etText.setText(str.getNote() + "");
            }
        }
//        MyState.
    }

    @Override
    public void getBackMemberList() {
        HttpCent.getInstance(this).getRevisitTypeList(this, MyState.requestTwo);
    }

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        switch (order) {
            case MyState.requestTwo:
                try {
                    List<BackTypeBean> l = JsonUtil.getListByJsonString(data.toString(), BackTypeBean.class);
                    list.clear();
                    list.addAll(l);
                    adapter.notifyDataSetChanged();

                } catch (Exception e) {
                    e.printStackTrace();
                }

                break;
            case MyState.requestFive:
                try {
                    MyUtils.mToast(CustomersActivity.this, "发送成功");
                    finish();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                break;
        }
    }


}

interface initMView {
    void getBackMemberList();//获取返回的访客类型列表
}